﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data.Common;
using System.Security.Cryptography; 

namespace LigerMVC.Common
{

    /// <summary>
    /// 帮助类
    /// </summary>
    public class DataHelper
    { 
        #region 深度拷贝对象
        /// <summary>
        /// DeepClone
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object Clone(object obj)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, obj);
                stream.Position = 0L;
                return formatter.Deserialize(stream);
            }
        }
        #endregion

        #region 数据转换

        /// <summary>
        /// 转换列表数据
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TTarget"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static IEnumerable<TTarget> ConvertList<TSource,TTarget>(List<TSource> list)
        {
            var target = new List<TTarget>(); 
            foreach (var item in list)
            {
                target.Add((TTarget)ConvertValue(typeof(TTarget), item));
            } 
            return (IEnumerable<TTarget>)target;
        }
        /// <summary>
        /// 列表数据 转换为可枚举的泛型对象
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="list"></param>
        /// <param name="targetType"></param> 
        /// <returns></returns>
        public static IEnumerable ListDataToEnumerable<TSource>(List<TSource> list, Type targetType)
        {
            IEnumerable target;
            if (targetType == typeof(int))
            {
                target = ConvertList<TSource, int>(list) as IEnumerable;
            }
            else if (targetType == typeof(double))
            {
                target = ConvertList<TSource, double>(list) as IEnumerable;
            }
            else if (targetType == typeof(decimal))
            {
                target = ConvertList<TSource, decimal>(list) as IEnumerable;
            }
            else if (targetType == typeof(string))
            {
                target = ConvertList<TSource, string>(list) as IEnumerable;
            }
            else if (targetType == typeof(byte[]))
            {
                target = ConvertList<TSource, byte[]>(list) as IEnumerable;
            }
            else
            {
                target = ConvertList<TSource, object>(list) as IEnumerable;
            }
            return target;
        }
        /// <summary>
        /// CheckStuct
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static bool CheckStruct(Type type)
        {
            return ((type.IsValueType && !type.IsEnum) && (!type.IsPrimitive && !type.IsSerializable));
        }
        /// <summary>
        /// 转换数据
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object ConvertValue(Type type, object value)
        {
            if (Convert.IsDBNull(value) || (value == null))
            {
                return null;
            }
            if (CheckStruct(type))
            {
                string data = value.ToString();
                return SerializationManager.Deserialize(type, data);
            }
            Type type2 = value.GetType();
            if (type == type2)
            {
                return value;
            }
            if (((type == typeof(Guid)) || (type == typeof(Guid?))) && (type2 == typeof(string)))
            {
                if (string.IsNullOrEmpty(value.ToString()))
                {
                    return null;
                }
                return new Guid(value.ToString());
            }
            if (((type == typeof(DateTime)) || (type == typeof(DateTime?))) && (type2 == typeof(string)))
            {
                if (string.IsNullOrEmpty(value.ToString()))
                {
                    return null;
                }
                return Convert.ToDateTime(value);
            }
            if (type.IsEnum)
            {
                try
                {
                    return Enum.Parse(type, value.ToString(), true);
                }
                catch
                {
                    return Enum.ToObject(type, value);
                }
            }
            if (((type == typeof(bool)) || (type == typeof(bool?))))
            {
                bool tempbool = false;
                if (bool.TryParse(value.ToString(), out tempbool))
                {
                    return tempbool;
                }
                else
                { 
                    if (string.IsNullOrEmpty(value.ToString()))
                        return false;
                    else
                        return true;
                } 
            } 
            if (type.IsGenericType)
            {
                type = type.GetGenericArguments()[0];
            } 
            return Convert.ChangeType(value, type);
        }
        /// <summary>
        /// 转换数据类型
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TResult ConvertValue<TResult>(object value)
        {
            if (Convert.IsDBNull(value) || value == null)
                return default(TResult);

            object obj = ConvertValue(typeof(TResult), value);
            if (obj == null)
            {
                return default(TResult);
            }
            return (TResult)obj;
        }
        #endregion 

    }
}
